package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.myapplication.Fragment.FirstPageFragment;
import com.example.myapplication.R;
import com.example.myapplication.goods.GoodsFragment;
import com.example.myapplication.Fragment.MeFragment;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
RadioGroup mainRg;
List<Fragment> fList;
FragmentManager fragmentManager;
private Button syBtn,typeBtn,meBtn,logBtn,regBtn,exitBtn;
TextView logTv;
DrawerLayout drawerLayout;
RadioButton syRb,typeRb,meRb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg=findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);//设置RadioGroup监听事件
        initFrag();
        initView();
    }
//将左边的抽屉布局的控件初始化
    private void initView() {
        syRb=findViewById(R.id.main_rb_fpage);
        typeRb=findViewById(R.id.main_rb_prod);
        meRb=findViewById(R.id.main_rb_me);
        syBtn =findViewById(R.id.main_left_btn_fp);
        meBtn =findViewById(R.id.main_left_btn_me);
        typeBtn =findViewById(R.id.main_left_btn_type);
        logBtn =findViewById(R.id.main_left_btn_login);
        regBtn =findViewById(R.id.main_left_btn_reg);
        exitBtn =findViewById(R.id.main_left_btn_exit);
        logTv =findViewById(R.id.main_left_tv1);
        drawerLayout = findViewById(R.id.main_drawer);
        syBtn.setOnClickListener(this);
        meBtn.setOnClickListener(this);
        typeBtn.setOnClickListener(this);
        logBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);
        exitBtn.setOnClickListener(this);

    }

    //切换Fragment的方法
    private void changeFrag(int pos){
        fragmentManager=getSupportFragmentManager();//碎片管理者
        FragmentTransaction ft = fragmentManager.beginTransaction();//将碎片放到Fragment中
        for (int i=0;i<fList.size();i++){
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();
    }
    //初始化
    private void initFrag(){
        fList=new ArrayList<>();
        fList.add(new FirstPageFragment());
        fList.add(new GoodsFragment());
        fList.add(new MeFragment());
        fragmentManager=getSupportFragmentManager();//碎片管理者
        FragmentTransaction ft = fragmentManager.beginTransaction();//将碎片放到Fragment中
        for (int i=0;i<fList.size();i++){
            ft.add(R.id.main_layout,fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.main_rb_fpage:
                changeFrag(0);
                break;
            case R.id.main_rb_prod:
                changeFrag(1);
                break;
            case R.id.main_rb_me:
                changeFrag(2);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_left_btn_fp:
                syRb.setChecked(true);
                break;
            case R.id.main_left_btn_type:
                typeRb.setChecked(true);
                break;
            case R.id.main_left_btn_me:
                meRb.setChecked(true);
                break;
            case R.id.main_left_btn_login:
                break;
            case R.id.main_left_btn_reg:
                break;
            case R.id.main_left_btn_exit:
                break;
        }
        drawerLayout.closeDrawers();//关闭抽屉
    }
}